﻿namespace Ruirun.Mes
{
    public partial class MesService
    {
        /// <summary>
        /// 获取 实体仓储对象
        /// </summary>
        private IPNStore IPNStore => ServiceProvider.GetService<IPNStore>();

        /// <summary>
        /// 获取 实体查询数据集
        /// </summary>
        public IQueryable<IPN> IPNs => IPNStore.Entities;

        /// <summary>
        /// 根据编号查找
        /// </summary>
        /// <param name="id">数据实体编号</param>
        /// <returns>业务操作结果</returns>
        public async Task<OperationResult<IPNOutputDto>> FindIPNById(Guid id)
        {
            Check.NotEmpty(id, nameof(id));

            var outputDto = IPNs.Where(o => o.Id.Equals(id)).ToOutput<IPN, IPNOutputDto>().FirstOrDefault();
            if (outputDto != null)
            {
                return await Task.FromResult(new OperationResult<IPNOutputDto>(OperationResultType.Success, "OK", outputDto));
            }

            return await Task.FromResult(new OperationResult<IPNOutputDto>(OperationResultType.NotFound, "Not Found", outputDto));
        }

        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="dtos">DTO信息</param>
        /// <returns>业务操作结果</returns>
        public async Task<OperationResult> CreateIPNs(params IPNInputDto[] dtos)
        {
            return await IPNStore.InsertByDtoAsync(dtos);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="dtos">DTO信息</param>
        /// <returns>业务操作结果</returns>
        public async Task<OperationResult> UpdateIPNs(params IPNInputDto[] dtos)
        {
            return await IPNStore.UpdateByDtoAsync(dtos);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity">实体信息</param>
        /// <returns>业务操作结果</returns>
        public async Task<OperationResult> UpdateIPN(IPN entity)
        {
            return await IPNStore.UpdateAsync(entity);
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="ids">数据实体编号</param>
        /// <returns>业务操作结果</returns>
        public async Task<OperationResult> DeleteIPNs(params Guid[] ids)
        {
            return await IPNStore.DeleteAsync(ids);
        }

        //public async Task<OperationResult> ApplyIPNHandler(Guid id, Status target)
        //{
        //    OperationResult operation = OperationResult.Error("操作失败");

        //    var ipn = IPNStore.Entities.FirstOrDefault(a => a.Id == id);
        //    if (ipn == null) return OperationResult.Error("当前申请单不存在");

        //    if (ipn.Status == Status.CompleteApply) return OperationResult.Error("当前单据已完成，无法执行其它操作");

        //    switch (target)
        //    {
        //        case Status.WaitApply:
        //            if (ipn.Status != Status.RdApprove ) return OperationResult.Error("单据状态不符当前操作");
        //            ipn.Status = target;
        //            break;
        //        case Status.RdApprove:
        //            if (ipn.Status != Status.WaitApply && ipn.Status != Status.IpnApprove) return OperationResult.Error("单据状态不符当前操作");
        //            ipn.Status = target;
        //            break;
        //        case Status.IpnApprove:
        //            if (ipn.Status != Status.RdApprove) return OperationResult.Error("单据状态不符当前操作");
        //            ipn.Status = target;
        //            break;
        //        case Status.CompleteApply:
        //            if (ipn.Status != Status.IpnApprove) return OperationResult.Error("单据状态不符当前操作");
        //            ipn.Status = target;
        //            break;
        //        default:
        //            ipn.Status = target;
        //            break;
        //    }
        //    operation = await IPNStore.UpdateAsync(ipn);
        //    return operation;
        //}
      
    }
}